﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.QuocGiaService;
namespace eTMS.Controllers{
public class QuocGiaControllerBase : CommonController{
	public QuocGiaControllerBase()
		: base()
	{	
		this.dao = new QuocGiaServiceClient();
	}
	~QuocGiaControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private QuocGiaEntity oQuocGia;	
	public QuocGiaEntity QuocGiaObject{
		get{
			return oQuocGia;
		}
		set{
			oQuocGia=value;
		}
	}
	protected QuocGiaServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSQuocGia(false);			
			return _Table;
		}
	}
	public void LoadAllDSQuocGia(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oQuocGia==null)return false;
		bool result=true;
		try{
			oQuocGia = dao.SaveRet(oQuocGia);
		}
		catch{
			result=false;
		}
		if(result){
			_ListQuocGia.Add(oQuocGia);
		}
		return result;
	}
	public bool Update(){
		if(oQuocGia==null)return false;
		return dao.Update(oQuocGia);
	}
	public bool Delete(){
		if(oQuocGia==null)return false;
		bool retvalue = dao.Delete(oQuocGia);
		if(retvalue){			
			_ListQuocGia.Remove(oQuocGia);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 QuocGia_ID){
		bool retvalue = dao.DeleteByID(QuocGia_ID);		
		if(retvalue){
			QuocGiaEntity oDeleted=FindQuocGia(QuocGia_ID);
			if(oDeleted!=null){
				_ListQuocGia.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oQuocGia = new QuocGiaEntity();
	}
	//Check if exists MaQuocGia
	public bool CheckExistsMaQuocGia(object value){	
		return dao.SelectOneByMaQuocGia(value.ToString()).MaQuocGia!=null;
    }    
    //Copy data from data row in grid to object
    public QuocGiaEntity MapDataRowToObject(DataRow row){
		QuocGiaEntity o = new QuocGiaEntity();
		 o.MaQuocGia=Convert.ToString(row["MaQuocGia"]);
		 o.TenQuocGia=Convert.ToString(row["TenQuocGia"]);
		 o.MoTaQuocGia=Convert.ToString(row["MoTaQuocGia"]);
		 if(!row.IsNull("QuocGia_ID"))
		    o.QuocGia_ID=(Int32)(row["QuocGia_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(QuocGiaEntity o,DataRow row){
		row["MaQuocGia"]=o.MaQuocGia;
		row["TenQuocGia"]=o.TenQuocGia;
		row["MoTaQuocGia"]=o.MoTaQuocGia;
		row["QuocGia_ID"]=o.QuocGia_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.QuocGiaObject,row);
    }
	public void LoadObject(Int32 QuocGia_ID){
		QuocGiaEntity oLoad=FindQuocGia(QuocGia_ID);
		if(oLoad==null){
			oQuocGia=this.LoadQuocGia(QuocGia_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oQuocGia.IsNullQuocGia_ID;
			if(!bNotIsNull){
				this.ListQuocGia.Add(oQuocGia);
			}
		}
		else{
			oQuocGia=oLoad;
		}
	}
	#region CachedList QuocGia
	private static List<QuocGiaEntity> _ListQuocGia=new List<QuocGiaEntity>();
    public List<QuocGiaEntity>ListQuocGia{
		get{
			return _ListQuocGia;
		}
	}
	public static List<QuocGiaEntity>ListQuocGiaStatic{
		get{
			return _ListQuocGia;
		}
	}
	public QuocGiaEntity FindQuocGia(Int32 QuocGia_ID){
		QuocGiaEntity oResult=this.ListQuocGia.Find(delegate(QuocGiaEntity oFind){
			bool result=true; 
			result&=oFind.QuocGia_ID.Equals(QuocGia_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllQuocGia(bool clearCache){
		if(_ListQuocGia.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		QuocGiaServiceClient dao = new QuocGiaServiceClient();
		_ListQuocGia=new List<QuocGiaEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public QuocGiaEntity LoadQuocGia(Int32 QuocGia_ID){			
		QuocGiaEntity o=dao.SelectOne(QuocGia_ID);		
		return o;
	}
	#endregion
}
}
